package com.mengxuegu.springboot.Controller;

import com.mengxuegu.springboot.entities.User;
import com.mengxuegu.springboot.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.Map;

@Controller
public class LoginController {
    @Autowired
    UserMapper userMapper;
    @PostMapping("/login")
    public String login(HttpSession session,
                        String username ,
                        String password ,
                        Map<String,Object> map){
        if(!StringUtils.isEmpty(username)  ){
            User userByUsername = userMapper.getUserByUsername(username);
            if (userByUsername != null &&  userByUsername.getPassword().equals(password)){
                session.setAttribute("loginUser", userByUsername.getUsername());
                session.setAttribute("sessionuserid",userByUsername.getId());
                session.setAttribute("sessionuser",userByUsername);

                return "redirect:/main.html";
            }
            map.put("msg","用户名或密码错误");
        }
        return "main/login";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");
        session.invalidate();
        return "redirect:/index.html";
    }

}
